<?php
namespace app\admin\controller;

use app\common\model\Movies as MoviesModel;
use app\common\controller\AdminBase;

/**
 * 电影
 * Class Link
 * @package app\admin\controller
 */
class Movies extends AdminBase
{
    protected $MoviesModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->MoviesModel = new MoviesModel();
    }

    /**
     * 电影
     * @return mixed
     */
    public function index()
    {
        $movieList = $this->MoviesModel->paginate(30);

        // 获取分页显示
        $page = $movieList->render();
        return $this->fetch('index', ['data' => $movieList,'page'=>$page,'category'=>cache('category')]);
    }

    /**
     * 添加电影
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存电影
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'Link');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->MoviesModel->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑电影
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $link = $this->MoviesModel->find($id);

        return $this->fetch('edit', ['link' => $link]);
    }

    /**
     * 更新电影
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'Link');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->MoviesModel->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除电影
     * @param $id
     */
    public function delete($id)
    {
        if ($this->MoviesModel->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}